package com.ipzoe.web.controller.mem;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ipzoe.common.utils.SecurityUtils;
import com.ipzoe.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ipzoe.common.annotation.Log;
import com.ipzoe.common.core.controller.BaseController;
import com.ipzoe.common.core.domain.AjaxResult;
import com.ipzoe.common.enums.BusinessType;
import com.ipzoe.mem.domain.Dealer;
import com.ipzoe.mem.service.IDealerService;
import com.ipzoe.common.utils.poi.ExcelUtil;
import com.ipzoe.common.core.page.TableDataInfo;

/**
 * 经销商Controller
 * 
 * @author eric
 * @date 2020-08-20
 */
@RestController
@RequestMapping("/member/dealer" )
public class DealerController extends BaseController {

    @Autowired
    private IDealerService iDealerService;

    /**
     * 查询经销商列表
     */
    @PreAuthorize("@ss.hasPermi('member:dealer:list')")
    @GetMapping("/list")
    public TableDataInfo list(Dealer dealer)
    {
        startPage();
        LambdaQueryWrapper<Dealer> lqw = new LambdaQueryWrapper<Dealer>();
        List<Dealer> list = iDealerService.list(lqw);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('member:dealer:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(Dealer dealer)
    {
        return AjaxResult.success(iDealerService.list());
    }

    /**
     * 导出经销商列表
     */
    @PreAuthorize("@ss.hasPermi('member:dealer:export')" )
    @Log(title = "经销商" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Dealer dealer) {
        LambdaQueryWrapper<Dealer> lqw = new LambdaQueryWrapper<Dealer>(dealer);
        List<Dealer> list = iDealerService.list(lqw);
        ExcelUtil<Dealer> util = new ExcelUtil<Dealer>(Dealer. class);
        return util.exportExcel(list, "dealer" );
    }

    /**
     * 获取经销商详细信息
     */
    @PreAuthorize("@ss.hasPermi('member:dealer:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iDealerService.getById(id));
    }

    /**
     * 新增经销商
     */
    @PreAuthorize("@ss.hasPermi('member:dealer:add')" )
    @Log(title = "经销商" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dealer dealer) {
        dealer.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iDealerService.save(dealer) ? 1 : 0);
    }

    /**
     * 修改经销商
     */
    @PreAuthorize("@ss.hasPermi('member:dealer:edit')" )
    @Log(title = "经销商" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dealer dealer) {
        dealer.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iDealerService.updateById(dealer) ? 1 : 0);
    }

    /**
     * 删除经销商
     */
    @PreAuthorize("@ss.hasPermi('member:dealer:remove')" )
    @Log(title = "经销商" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iDealerService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
